package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.CmsCategory;
import com.example.demo.common.Respones;
import com.example.demo.service.CmsCategoryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;


@Api(tags = "栏目管理服务")
@RestController
@RequestMapping("/category")
public class CmsCategoryController {
	
	@Autowired
	private CmsCategoryService categoryservice;
	
	@ApiOperation(value = "保存或者更新栏目信息",
			notes = "如果传递参数时没有指明栏目编号，那么执行插入操作")
	@PostMapping("/saveOrUpdateCategory")
	public Respones<String> saveOrUpdate(CmsCategory category){
	boolean b= categoryservice.saveOrUpdateCmsCategory(category);
	if(b)	return Respones.ok("操作成功!!!");
	else return Respones.error("操作失败!");
		
	}
	
	
	
	
	@ApiOperation(value = "获取所有栏目信息",notes="从数据库检索所有栏目信息")
	@GetMapping("/all")
	public Respones<List<CmsCategory>> queryAll(){
		//调用业务方法
		List<CmsCategory> cs=categoryservice.getAllCategory();
		return Respones.ok(cs);
	}

	
	@ApiOperation(value = "按照栏目编号删除栏目信息")
	@ApiImplicitParam(name = "id",value = "栏目编号",required = true)
	@DeleteMapping("/deleteById")
	public Respones<String> deleteById(String id){
		int num =categoryservice.deleteById(id);
		if(num==-1) {
			return Respones.error("请先删除所有子栏目");
		}
		if(num==-2) {
			return Respones.error("请先删除该栏目下所有文件");
		}
		if(num==0) {
			return Respones.error("此栏目不存在");
		}
		
		return Respones.ok("删除成功！" );	
	}
	
	@ApiOperation(value = "批量删除栏目信息！")
	@DeleteMapping("/batchDeleteByIds")
	public Respones<String > deleteByIds(String[] ids){
		String result=
				categoryservice.batchDelete(ids);
		if(result.startsWith("success")) {
			return Respones.ok(result);
		}else
		{return Respones.error(result);}
	}
	
	
}
